package com.gidgabs.modules.api.controller.chat;

import com.gidgabs.common.exception.RRException;
import com.gidgabs.common.utils.ReturnJson;
import com.gidgabs.dto.TbUserDto;
import com.gidgabs.utils.SessionDataUtil;
import com.gidgabs.utils.YunXinUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 * Created by tby on 2018/7/16.
 */
@Api("聊天模块")
@RestController
@RequestMapping("api/chat")
public class ChatController {



    @ApiOperation(value = "查询群聊消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tid",value = "群id",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "startTime",value = "开始时间",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "endTime",value = "结束时间",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "查询条数",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "reverse",value = "排序",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "type",value = "类型",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "token",value = "token",dataType = "string",paramType = "header")
    })
    @RequestMapping(value = "queryTeamMsg", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public ReturnJson queryTeamMsg(String tid, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, Integer limit, Integer reverse, String type ) {
        if (StringUtils.isBlank(tid)){
            return ReturnJson.fail("tid参数不能为空");
        }
        if (startTime==null){
            return ReturnJson.fail("查询开始时间不能未空");
        }
        if (endTime==null){
            return ReturnJson.fail("查询结束时间不能未空");
        }if (limit>100||limit<1){
            return ReturnJson.fail("查询条数参数错误");
        }if (reverse==null){
            reverse=2;
        }
        String accid = String.valueOf(SessionDataUtil.getCurrentUserId());
        Map<String, Object> map = YunXinUtil.queryTeamMsg(tid,accid ,startTime, endTime, limit, reverse, type);
        if (map.get("code").equals(200)){
            return ReturnJson.success("查询成功",(List) map.get("msgs"),(int)(map.get("size")));
        }else {
            return ReturnJson.fail("查询失败",map);
        }
    }


    @ApiOperation(value = "查询单聊消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "from",value = "发送者",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "to",value = "接收者",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "startTime",value = "开始时间",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "endTime",value = "结束时间",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "查询条数",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "reverse",value = "排序",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "type",value = "类型",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "token",value = "token",dataType = "string",paramType = "header")
    })
    @RequestMapping(value = "querySessionMsg", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public ReturnJson querySessionMsg(String from,String to, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, Integer limit, Integer reverse, String type) {
        if (StringUtils.isBlank(from)){
            return ReturnJson.fail("from参数不能为空");
        }
        if (StringUtils.isBlank(to)){
            return ReturnJson.fail("to参数不能为空");
        }
        if (startTime==null){
            return ReturnJson.fail("查询开始时间不能未空");
        }
        if (endTime==null){
            return ReturnJson.fail("查询结束时间不能未空");
        }if (limit>100||limit<1){
            return ReturnJson.fail("查询条数参数错误");
        }if (reverse==null){
            reverse=2;
        }
        Map<String, Object> map = YunXinUtil.querySessionMsg(from,to ,startTime, endTime, limit, reverse, type);
        if (map.get("code").equals(200)){
            return ReturnJson.success("查询成功",(List) map.get("msgs"),(int)(map.get("size")));
        }else {
            return ReturnJson.fail("查询失败");
        }
    }
}
